package com.baidumap.administrator.happystudy.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ab.util.SharedPreferencesHelper;
import com.ab.view.simple_rating_bar.SimpleRatingBar;
import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.alipay.AlipayUtil;
import com.baidumap.administrator.happystudy.utils.GlideUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.baidumap.administrator.happystudy.R.id.simpleRatingBar;

@SuppressWarnings("all")
public class FragmentOrderWeiZhiFuListAdapter extends BaseAdapter {
    private List<Map<String, Object>> lists;
    private Activity context;
    private ViewHolder holder;
    private LayoutInflater inflater;
    private DisplayImageOptions options;
    public ArrayList<A> list;

    private SharedPreferencesHelper sHelper;

    public FragmentOrderWeiZhiFuListAdapter(Activity _Context, SharedPreferencesHelper sHelper) {

        this.context = _Context;
        this.sHelper = sHelper;

    }

    public void bindData(List<Map<String, Object>> list) {
        this.lists = list;
    }


    class ViewHolder {

        ImageView img_shop;
        TextView tv_shop_name;
        TextView tv_pei_song_by;
        TextView tv_sold;
        TextView tv_distance;
        TextView tv_qi_song_jia;
        TextView tv_li_ji_zhi_fu;
        SimpleRatingBar simpleRatingBar;
    }

    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public List<Map<String, Object>> getItem(int position) {
        return (List<Map<String, Object>>) lists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("NewApi")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final int index = position;
        String shop_id = null;
        String shop_name = null;
        if (convertView != null) {

            holder = (ViewHolder) view.getTag();

        } else {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.item_order_list_wei_zhi_fu, null);
            holder = new ViewHolder();

            holder.img_shop = (ImageView) convertView.findViewById(R.id.img_shop);
            holder.tv_shop_name = (TextView) convertView.findViewById(R.id.tv_shop_name);
            holder.tv_pei_song_by = (TextView) convertView.findViewById(R.id.tv_pei_song_by);
            holder.tv_sold = (TextView) convertView.findViewById(R.id.tv_sold);
            holder.tv_qi_song_jia = (TextView) convertView.findViewById(R.id.tv_qi_song_jia);
            holder.tv_distance = (TextView) convertView.findViewById(R.id.tv_distance);
            holder.tv_li_ji_zhi_fu = (TextView) convertView.findViewById(R.id.tv_li_ji_zhi_fu);
            holder.simpleRatingBar = (SimpleRatingBar) convertView.findViewById(simpleRatingBar);


            holder.tv_shop_name.setText((String) lists.get(position).get("shop_name"));
            holder.tv_pei_song_by.setText("自己配送");
            holder.tv_sold.setText((String) "月售" + lists.get(position).get("product_num") + "单");
            holder.tv_qi_song_jia.setText("￥ " + (String) lists.get(position).get("qi_song_jia"));
            holder.tv_distance.setText((String) lists.get(position).get("distance") + "km");
            holder.simpleRatingBar.setRating(Float.parseFloat((String) lists.get(position).get("shop_rating")));
            holder.simpleRatingBar.setClickable(false);

            GlideUtil.displayImg(context, lists.get(position).get("product_img").toString(), holder.img_shop);

            final String price = (String) lists.get(position).get("qi_song_jia");
            final String order_no = (String) lists.get(position).get("order_no");
            final String status = (String) lists.get(position).get("status");

            if (status.equals("0")) {
                holder.tv_li_ji_zhi_fu.setVisibility(View.VISIBLE);
                holder.tv_li_ji_zhi_fu.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new AlipayUtil(context, price, order_no, "", sHelper).alipay();
                    }
                });
            } else {
                holder.tv_li_ji_zhi_fu.setVisibility(View.GONE);
            }

            convertView.setTag(holder);
        }

        return convertView;
    }

    public class A {

        public static final int TYPE_CHECKED = 1;
        public static final int TYPE_NOCHECKED = 0;

        public boolean type;
        public String phone;

        public A(boolean type, String phone) {
            this.type = type;
            this.phone = phone;
        }
    }


}
